from PyQt6.QtCore import Qt, QRect, QRectF
from PyQt6.QtGui import QPainter
from PyQt6.QtWidgets import QWidget

from gui.utils import get_pixmap, FLAG_ALIGN_CENTER, format_number
from gui.utils.colors import COLOR_LESS
from gui.utils.size import TECH_WIDTH, TECH_HEIGHT, RESOURCE_HEIGHT, WINDOW_WIDTH, DX, ZONING_SIZE
from settings import IMAGE_DIR
from state.game_state import GameState
from state.tech_panel import TechProduce
from utils.event_bus import EventBus

TechList = ('physics', 'engineer', 'beyond')


class PanelTechnology(QWidget):
    def __init__(self, state: GameState, parent=None):
        super(PanelTechnology, self).__init__(parent)
        self.w = TECH_WIDTH
        self.h = TECH_HEIGHT
        rect = QRectF(WINDOW_WIDTH - TECH_WIDTH, RESOURCE_HEIGHT, TECH_WIDTH, ZONING_SIZE)
        self.setGeometry(rect.toRect())

        self.state: GameState = state
        self.tech_panel = self.state.players[self.state.obverse].tech_panel

        EventBus.register('update_panel_technology', self.update)
        EventBus.register('update_tech_rate', self.update_tech_rate)

    def paintEvent(self, event):
        painter = QPainter(self)
        for index, tech in enumerate(TechList):
            sy = TECH_HEIGHT * index
            r: TechProduce = getattr(self.tech_panel, tech)
            half_h = TECH_HEIGHT / 2

            # 进度条
            painter.setPen(COLOR_LESS)
            painter.setBrush(Qt.GlobalColor.white)
            painter.drawRect(QRectF(half_h, sy + half_h, TECH_WIDTH, half_h))

            sche_total_width = TECH_WIDTH - half_h
            painter.drawRect(rect := QRectF(half_h, sy + half_h, sche_total_width, half_h))

            sche_width = (r.current_point / r.target_point) * sche_total_width
            if sche_width > 0:
                painter.setBrush(r.color)
                painter.drawRect(QRectF(half_h, sy + half_h, sche_width, half_h + 2))

            painter.setPen(Qt.GlobalColor.black)
            painter.setBrush(Qt.GlobalColor.white)
            painter.drawText(rect, FLAG_ALIGN_CENTER, r.current_tech_name or '未选择')

            # 图标
            rect = QRectF(0.0, sy, half_h, half_h)
            painter.drawRect(rect)
            icon_path = str(IMAGE_DIR / 'icon' / f'{tech}.png')
            painter.drawPixmap(rect.toRect(), get_pixmap(icon_path))
            # 比例
            painter.drawRect(rect := QRectF(0.0, sy + half_h, half_h, half_h))
            painter.drawText(rect, FLAG_ALIGN_CENTER, str(self.tech_panel.rate[tech]))
            # 库存
            painter.drawRect(rect := QRectF(half_h, sy, hw := (TECH_WIDTH - half_h) / 2, half_h))
            painter.drawText(rect, FLAG_ALIGN_CENTER, format_number(r.storage))
            # 收入
            painter.drawRect(rect := QRectF(half_h + hw, sy, hw, half_h))
            painter.drawText(rect, FLAG_ALIGN_CENTER, ('+' if r.daily > 0 else '') + format_number(r.daily))

    def update_tech_rate(self, rate: str):
        p, e, b = rate.split(',')
        self.tech_panel.rate = {
            'physics': int(p),
            'engineer': int(e),
            'beyond': int(b)
        }
        self.update()
